package com.twenty.seven.demo.demo.controller;

import com.twenty.seven.common.ResultJson;
import com.twenty.seven.common.enums.StatusCode;
import com.twenty.seven.common.exception.CustomException;
import com.twenty.seven.demo.demo.service.UserService;
import com.twenty.seven.entity.User;
import com.twenty.seven.exception.CMSException;
import com.twenty.seven.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 001
 * @Date 2020/1/6 15:31
 * @TODO
 **/
@RestController
@RequestMapping
public class Demo {

    @Value("${hello}")
    private String hello;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "get",method = RequestMethod.GET)
    public String getHello(){
        return hello;
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public User save(@RequestBody User user){
        return userService.save(user);
    }

    @GetMapping(value = "/query/{id}")
    public ResultJson query(@PathVariable(value = "id")long id) {
        ResultJson resultJson;
        try {
            resultJson = new ResultJson(StatusCode.success.getCode(),StatusCode.success.getName(),userService.queryUserById(id));
        } catch (CustomException e) {
            e.printStackTrace();
            resultJson = new ResultJson(e.getCode(),e.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            resultJson = new ResultJson(StatusCode.systemError.getCode(),StatusCode.systemError.getName());
        }
        return resultJson;
    }

    @GetMapping(value = "/wang")
    public Result testException(){
        throw new NullPointerException();
//        return Result.ok();
    }
}
